package org.apache.http.message;

import org.apache.http.HeaderElement;
import org.apache.http.NameValuePair;
import org.apache.http.a.a;
import org.apache.http.annotation.Immutable;

@Immutable
public class d implements HeaderValueFormatter {
    @Deprecated
    public static final d a = new d();
    public static final d b = new d();

    protected int a(HeaderElement headerElement) {
        int i = 0;
        if (headerElement == null) {
            return 0;
        }
        int length = headerElement.getName().length();
        String value = headerElement.getValue();
        if (value != null) {
            length += value.length() + 3;
        }
        int parameterCount = headerElement.getParameterCount();
        if (parameterCount <= 0) {
            return length;
        }
        while (i < parameterCount) {
            length += a(headerElement.getParameter(i)) + 2;
            i++;
        }
        return length;
    }

    protected int a(NameValuePair nameValuePair) {
        if (nameValuePair == null) {
            return 0;
        }
        int length = nameValuePair.getName().length();
        String value = nameValuePair.getValue();
        return value != null ? length + (value.length() + 3) : length;
    }

    protected int a(HeaderElement[] headerElementArr) {
        int i = 0;
        if (headerElementArr != null && headerElementArr.length >= 1) {
            int length = (headerElementArr.length - 1) * 2;
            i = length;
            length = 0;
            while (length < headerElementArr.length) {
                int a = a(headerElementArr[length]) + i;
                length++;
                i = a;
            }
        }
        return i;
    }

    protected int a(NameValuePair[] nameValuePairArr) {
        int i = 0;
        if (nameValuePairArr != null && nameValuePairArr.length >= 1) {
            int length = (nameValuePairArr.length - 1) * 2;
            i = length;
            length = 0;
            while (length < nameValuePairArr.length) {
                int a = a(nameValuePairArr[length]) + i;
                length++;
                i = a;
            }
        }
        return i;
    }

    protected void a(org.apache.http.a.d dVar, String str, boolean z) {
        int i = 0;
        if (!z) {
            for (int i2 = 0; i2 < str.length() && !r7; i2++) {
                z = a(str.charAt(i2));
            }
        }
        if (z) {
            dVar.a('\"');
        }
        while (i < str.length()) {
            char charAt = str.charAt(i);
            if (b(charAt)) {
                dVar.a('\\');
            }
            dVar.a(charAt);
            i++;
        }
        if (z) {
            dVar.a('\"');
        }
    }

    protected boolean a(char c) {
        return " ;,:@()<>\\\"/[]?={}\t".indexOf(c) >= 0;
    }

    protected boolean b(char c) {
        return "\"\\".indexOf(c) >= 0;
    }

    public org.apache.http.a.d formatElements(org.apache.http.a.d dVar, HeaderElement[] headerElementArr, boolean z) {
        a.a((Object) headerElementArr, "Header element array");
        int a = a(headerElementArr);
        if (dVar == null) {
            dVar = new org.apache.http.a.d(a);
        } else {
            dVar.a(a);
        }
        for (a = 0; a < headerElementArr.length; a++) {
            if (a > 0) {
                dVar.a(", ");
            }
            formatHeaderElement(dVar, headerElementArr[a], z);
        }
        return dVar;
    }

    public org.apache.http.a.d formatHeaderElement(org.apache.http.a.d dVar, HeaderElement headerElement, boolean z) {
        a.a((Object) headerElement, "Header element");
        int a = a(headerElement);
        if (dVar == null) {
            dVar = new org.apache.http.a.d(a);
        } else {
            dVar.a(a);
        }
        dVar.a(headerElement.getName());
        String value = headerElement.getValue();
        if (value != null) {
            dVar.a('=');
            a(dVar, value, z);
        }
        int parameterCount = headerElement.getParameterCount();
        if (parameterCount > 0) {
            for (a = 0; a < parameterCount; a++) {
                dVar.a("; ");
                formatNameValuePair(dVar, headerElement.getParameter(a), z);
            }
        }
        return dVar;
    }

    public org.apache.http.a.d formatNameValuePair(org.apache.http.a.d dVar, NameValuePair nameValuePair, boolean z) {
        a.a((Object) nameValuePair, "Name / value pair");
        int a = a(nameValuePair);
        if (dVar == null) {
            dVar = new org.apache.http.a.d(a);
        } else {
            dVar.a(a);
        }
        dVar.a(nameValuePair.getName());
        String value = nameValuePair.getValue();
        if (value != null) {
            dVar.a('=');
            a(dVar, value, z);
        }
        return dVar;
    }

    public org.apache.http.a.d formatParameters(org.apache.http.a.d dVar, NameValuePair[] nameValuePairArr, boolean z) {
        a.a((Object) nameValuePairArr, "Header parameter array");
        int a = a(nameValuePairArr);
        if (dVar == null) {
            dVar = new org.apache.http.a.d(a);
        } else {
            dVar.a(a);
        }
        for (a = 0; a < nameValuePairArr.length; a++) {
            if (a > 0) {
                dVar.a("; ");
            }
            formatNameValuePair(dVar, nameValuePairArr[a], z);
        }
        return dVar;
    }
}
